//
//  MyOrderViewController.m
//  WuJieLive
//
//  Created by iMac on 2017/12/20.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "MyOrderViewController.h"
#import "AllOrderController.h"
#import "WaitPayForController.h"
#import "WaitGetGoodsController.h"
#import "WaitSendGoodsController.h"
#import "WaitEvaluationController.h"

@interface MyOrderViewController ()<UIScrollViewDelegate>

@property (nonatomic , strong) AllOrderController * allVC;
@property (nonatomic , strong) WaitPayForController * waitPFVC;
@property (nonatomic , strong) WaitGetGoodsController * waitGGVC;
@property (nonatomic , strong) WaitSendGoodsController * waitSGVC;
@property (nonatomic , strong) WaitEvaluationController * waitEVC;
@property (nonatomic , strong) UIViewController * currentVC;
@property (nonatomic , strong) UIView * contentView;
@property (nonatomic , strong) HDSegmentMenu * menu;
@end

@implementation MyOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

-(void)setNavi
{
    self.title = @"我的订单";
}

-(void)setUI
{
    [self.view addSubview:self.menu];
    
    [self loadChildControllerWithIndex:self.indexString.length > 0 ? [self.indexString integerValue] : 0];
    
    __weak typeof(self) weakSelf = self;
    self.menu.segmentSelectedBlock = ^(NSInteger index) {
        switch (index) {
            case 0:
            {
                [weakSelf changeControllerFromOldController:weakSelf.currentVC toNewController:weakSelf.allVC];
            }
                break;
            case 1:
            {
                [weakSelf changeControllerFromOldController:weakSelf.currentVC toNewController:weakSelf.waitPFVC];
            }
                break;
            case 2:
            {
                [weakSelf changeControllerFromOldController:weakSelf.currentVC toNewController:weakSelf.waitSGVC];
            }
                break;
            case 3:
            {
                [weakSelf changeControllerFromOldController:weakSelf.currentVC toNewController:weakSelf.waitGGVC];
            }
                break;
            case 4:
            {
                [weakSelf changeControllerFromOldController:weakSelf.currentVC toNewController:weakSelf.waitEVC];
            }
                break;
            default:
                break;
        }
    };
}

- (void)loadChildControllerWithIndex:(NSInteger)index {
    _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 40, kScreenWidth , kScreenHeight - 40)];
    [self.view addSubview:_contentView];
    
    self.allVC = [AllOrderController new];
    [self.allVC.view setFrame:_contentView.bounds];
    
    self.waitPFVC = [WaitPayForController new];
    [self.waitPFVC.view setFrame:_contentView.bounds];
    
    self.waitSGVC = [WaitSendGoodsController new];
    [self.waitSGVC.view setFrame:_contentView.bounds];
    
    self.waitGGVC = [WaitGetGoodsController new];
    [self.waitGGVC.view setFrame:_contentView.bounds];
    
    self.waitEVC = [WaitEvaluationController new];
    [self.waitEVC.view setFrame:_contentView.bounds];
    
    switch (index) {
        case 0:
        {
            [self addChildViewController:self.allVC];
            [_contentView addSubview:self.allVC.view];
            _currentVC = self.allVC;
        }
            break;
        case 1:
        {
            [self addChildViewController:self.waitPFVC];
            [_contentView addSubview:self.waitPFVC.view];
            _currentVC = self.waitPFVC;
        }
            break;
        case 2:
        {
            [self addChildViewController:self.waitSGVC];
            [_contentView addSubview:self.waitSGVC.view];
            _currentVC = self.waitSGVC;
        }
            break;
        case 3:
        {
            [self addChildViewController:self.waitGGVC];
            [_contentView addSubview:self.waitGGVC.view];
            _currentVC = self.waitGGVC;
        }
            break;
        case 4:
        {
            [self addChildViewController:self.waitEVC];
            [_contentView addSubview:self.waitEVC.view];
            _currentVC = self.waitEVC;
        }
            break;
        default:
            break;
    }
}


- (void)changeControllerFromOldController:(UIViewController *)oldController toNewController:(UIViewController *)newController {
    [self addChildViewController:newController];
    [self transitionFromViewController:oldController toViewController:newController duration:0.3 options:UIViewAnimationOptionCurveEaseIn animations:^{
        
    } completion:^(BOOL finished) {
        if (finished) {
            [newController didMoveToParentViewController:self];
            [oldController willMoveToParentViewController:nil];
            [oldController removeFromParentViewController];
            _currentVC = newController;
        } else {
            _currentVC = oldController;
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(HDSegmentMenu *)menu
{
    if (!_menu) {
//        _menu = [[HDSegmentMenu alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 40) titlesArray:@[@"全部",@"待付款",@"待发货",@"待收货",@"待评价"] selectedColor:MainColor itemSpace:20 ];
        _menu = [[HDSegmentMenu alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 40) titlesArray:@[@"全部",@"待付款",@"待发货",@"待收货",@"待评价"] selectedColor:MainColor itemSpace:20 withSelectedIndex: self.indexString.length > 0 ? [self.indexString integerValue] : 0];
    }
    return _menu;
}

@end
